﻿using System;

namespace Core
{
    public static partial class Utils
    {
        public static class Time
        {
            //private static readonly long epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
            private static readonly long Epoch = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;

            /// <summary>
            /// 当前时间 单位：毫秒
            /// </summary>
            /// <returns></returns>
            public static long ClientNow()
            {
                return (DateTime.UtcNow.Ticks - Epoch) / 10000;
            }

            /// <summary>
            /// 当前时间 单位：秒
            /// </summary>
            /// <returns></returns>
            public static long ClientNowSeconds()
            {
                return (DateTime.UtcNow.Ticks - Epoch) / 10000000;
            }

            /// <summary>
            /// 当前时间，单位：毫秒
            /// 与ClientNow相同
            /// </summary>
            /// <returns></returns>
            public static long Now()
            {
                return ClientNow();
            }

            /// <summary>
            /// 格式化时间，将毫秒值转化为 {小时:分钟:秒}格式的的字符串
            /// </summary>
            /// <param name="ms"></param>
            public static string FormatMS(long ms)
            {
                long v = ms / 1000;
                long s = v % 60;
                v = (v - s) / 60;
                long m = v % 60;
                v = (v - m) / 60;

                string s_s = s < 10 ? $"0{s}" : $"{s}";
                string s_m = m < 10 ? $"0{m}" : $"{m}";

                if (v == 0)
                {
                    return $"{s_m}:{s_s}";
                }
                return $"{v}:{s_m}:{s_s}";
            }

            /// <summary>
            /// 格式化时间，将秒值转化为 {小时:分钟:秒}格式的的字符串
            /// </summary>
            /// <param name="s"></param>
            public static string FormatS(float s)
            {
                return FormatMS((long)(s * 1000));
            }

            private static int[] periodArr1 = new int[4];
            private static string[] periodArr2 = new string[] { "天", "小时", "分钟", "秒" };
            public static string Format(Period period, int num = 2)
            {
                periodArr1[0] = period.Day;
                periodArr1[1] = period.Hour;
                periodArr1[2] = period.Minute;
                periodArr1[3] = period.Second;

                string str = "";
                bool flag = false;
                int cnt = 0;
                for (int i = 0; i < 4; i++)
                {
                    if (periodArr1[i] > 0)
                    {
                        flag = true;
                    }

                    if (flag && cnt < num)
                    {
                        cnt++;
                        if (periodArr1[i] > 0)
                        {
                            str += $"{periodArr1[i]}{periodArr2[i]}";
                        }
                    }
                }

                return str;
            }

        }
    }
}
